package org.kyw.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.kyw.basic.exception.BusinessException;
import org.kyw.basic.util.AjaxResult;
import org.kyw.basic.util.PageList;
import org.kyw.user.domain.User;
import org.kyw.user.dto.UserDto;
import org.kyw.user.query.UserQuery;
import org.kyw.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理接口
 */

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/phone/register")
    public AjaxResult register(@RequestBody UserDto dto){
        try {
            userService.register(dto);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("报错了，正在殴打程序员");
        }
    }

    /**
     * 修改或新增用户
     */
    @PutMapping
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if(user.getId() == null){
                userService.add(user);
            }else {
                userService.update(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
     * 删除一条记录
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            userService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
     * 查询一条记录
     * @param id
     * @return
     */
    //@RequestMapping(value = "/1", method = RequestMethod.GET)
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id){
        return userService.queryById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<User> queryAll(){
        return userService.queryAll();
    }

    /**
     * 分页 + 高级查询
     * @return
     */
    @PostMapping("/list")
    public PageList<User> queryPage(@RequestBody UserQuery query){
        return userService.queryPage(query);
    }

}
